#ifndef __TCPCONNECTION_H_
#define __TCPCONNECTION_H_

#include "Noncopyable.hpp"
#include "InetAddress.hpp"
#include "Socket.hpp"
#include "SocketIO.hpp"
#include <functional>
#include <memory>
#include <string>

using std::function;
using std::shared_ptr;
using std::string;

namespace ln{

class TcpConnection;
using TcpConnectionPtr = shared_ptr<TcpConnection>;
using Callback = function<void(TcpConnectionPtr)>;

class TcpConnection
:Noncopyable
,public std::enable_shared_from_this<TcpConnection>
{

public:
    TcpConnection(int);
    //注册函数对象
    void setAllCallbacks(const Callback & cb1,const Callback & cb2,const Callback & cb3){
        _onConnection = cb1;
        _onMessage = cb2;
        _onClose = cb3;
    }
    string receive();
    void send(const string &);
    bool isClosed() const;
    string toString() const;

    //执行回调函数
    void handleNewConnectionCallback();
    void handleMessageCallback();
    void handleCloseCallback();

private:
    InetAddress getLocalAddr();
    InetAddress getPeerAddr();

private:
    Socket      _sock;
    SocketIO    _socketIO;
    InetAddress _localAddr;
    InetAddress _peerAddr;
    Callback    _onConnection;
    Callback    _onMessage;
    Callback    _onClose;
};

}//end of namespace ln

#endif

